﻿using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Mail
{
    public class SmtpMail
    {

        private SmtpClient sender = null;

        public SmtpMail()
        {
            sender = new SmtpClient();
        }

        public SmtpMail(string Host)
        {
            sender = new SmtpClient(Host);
        }

        public SmtpMail(string Host, string Username, string Password)
        {
            sender = new SmtpClient(Host);
            sender.Credentials = new System.Net.NetworkCredential(Username, Password);
        }

        public MailMessage CreateMessage(string from, string recipients, string subject)
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(from);

            foreach (MailAddress addr in GetEmailAddresses(recipients))
            {
                msg.To.Add(addr);
            }

            msg.IsBodyHtml = false;
            msg.Body = string.Empty;
            msg.Subject = subject;
            return msg;
        }

        public MailMessage CreateMessage(string from, string recipients, string CC, string subject)
        {
            MailMessage msg = CreateMessage(from, recipients, subject);

            foreach (MailAddress addr in GetEmailAddresses(CC))
            {
                msg.CC.Add(addr);
            }

            return msg;
        }

        public MailMessage CreateMessage(string from, string recipients, string CC, string Bcc, string subject)
        {
            MailMessage msg = CreateMessage(from, recipients, CC, subject);

            foreach (MailAddress addr in GetEmailAddresses(Bcc))
            {
                msg.Bcc.Add(addr);
            }

            return msg;
        }

        public HtmlMailMessage CreateHtmlMessage(string from, string recipients, string subject)
        {
            HtmlMailMessage msg = new HtmlMailMessage();
            msg.From = new MailAddress(from);

            foreach (MailAddress addr in GetEmailAddresses(recipients))
            {
                msg.To.Add(addr);
            }

            msg.IsBodyHtml = false;
            msg.Body = string.Empty;
            msg.Subject = subject;
            return msg;
        }

        public HtmlMailMessage CreateHtmlMessage(string from, string recipients, string CC, string subject)
        {
            HtmlMailMessage msg = CreateHtmlMessage(from, recipients, subject);

            foreach (MailAddress addr in GetEmailAddresses(CC))
            {
                msg.CC.Add(addr);
            }

            return msg;
        }

        public HtmlMailMessage CreateHtmlMessage(string from, string recipients, string CC, string Bcc, string subject)
        {
            HtmlMailMessage msg = CreateHtmlMessage(from, recipients, CC, subject);

            foreach (MailAddress addr in GetEmailAddresses(Bcc))
            {
                msg.Bcc.Add(addr);
            }

            return msg;
        }

        public void SendMessage(MailMessage Message, MailPriority Priority)
        {
            HtmlMailMessage msg = new HtmlMailMessage();
            msg.Body = "";
            Message.Priority = Priority;            
            sender.Send(Message);
        }

        public void SendMessage(HtmlMailMessage Message, MailPriority Priority)
        {
            Message.Body = Message.BodyHtml;
            Message.Priority = Priority;
            sender.Send(Message);
        }

        public void SendMessage(string from, string recipients, string subject, string body, MailPriority Priority)
        {
            MailMessage message = CreateMessage(from, recipients, subject);
            message.Body = body;
            message.Priority = Priority;
            sender.Send(message);
        }

        private MailAddressCollection GetEmailAddresses(string value)
        {
            MailAddressCollection coll = new MailAddressCollection();

            if ((!value.Contains(",")) && (!value.Contains(";")))
            {
                coll.Add(new MailAddress(value));
            }
            else
            {
                char delimiter = ';';
                if (value.Contains(",")) { delimiter = ','; }
                string[] mailto = value.Split(delimiter);

                foreach (string s in mailto)
                {
                    if (s.Trim() != string.Empty) { coll.Add(new MailAddress(s)); }
                }
            }

            return coll;
        }
    }
}
